package org.jeecg.modules.water.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.water.entity.DailyData;
import org.jeecg.modules.water.entity.LwgyJointWater;
import org.jeecg.modules.water.entity.LwgyJointWaterConfiguration;
import org.jeecg.modules.water.entity.LwgyJointWaterDaily;
import org.jeecg.modules.water.service.ILwgyJointWaterConfigurationService;
import org.jeecg.modules.water.service.ILwgyJointWaterDailyService;
import org.jeecg.modules.water.service.ILwgyJointWaterService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class SysnWaterDateJob implements Job {


    @Autowired
    private ILwgyJointWaterService lwgyJointWaterService;
    @Autowired
    private ILwgyJointWaterDailyService lwgyJointWaterDailyService;
    @Autowired
    private ILwgyJointWaterConfigurationService lwgyJointWaterConfigurationService;

    //水控-根据每日抄表数计算每日用水量，查询的是前天的数据（每日凌晨，计算昨天的用水平均值-前天的用水平均值），
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        LocalDate today = LocalDate.now(); // 获取今天的日期
        LocalDate yesterday = today.minusDays(1);//  获取昨天的日期
        LocalDate dayBeforeYesterday  = today.minusDays(2);//  获取前天的日期
        System.out.println("Today: " + today);
        System.out.println("Yesterday: " + yesterday);
        //获取所有表的抄表信息（今天，前天查询出来是昨天前天抄表的数据，比如，nb:前天17：00-昨天17：00；lora（定时器在23：00） 前天23：00-昨天23：00）
        List<DailyData> infolist = lwgyJointWaterService.getInfoByDate(dayBeforeYesterday.toString(),today.toString());
        System.out.println("infolist："+infolist);
        //获取所有表信息

        List<LwgyJointWaterConfiguration> list = lwgyJointWaterConfigurationService.list();

        List<LwgyJointWaterDaily> dailyList = new ArrayList<>();

        System.out.println("list："+list);
        if(list.size()>0){
            for(LwgyJointWaterConfiguration configuration : list){
                if(configuration.getCommunicationCode()!=null && !configuration.getCommunicationCode().equals("") && !configuration.getCommunicationCode().equals("null")) {
                    LwgyJointWaterDaily daily = new LwgyJointWaterDaily();
                    daily.setIspid(configuration.getCommunicationCode());
                    daily.setDailyWaterAverage("0");
                    daily.setDailyWaterMax("0");
                    daily.setDataTime(dayBeforeYesterday + "~" + yesterday);
                    if (infolist.size() > 0) {
                        // List<LwgyBaseSchool> collect =set.stream().filter(lwgyBaseSchool1 -> lwgyBaseSchool1.getId().equals(pid)).collect(Collectors.toList());

                        List<DailyData> xlist = infolist.stream().filter(water -> water.getIspid().equals(configuration.getCommunicationCode())).collect(Collectors.toList());

                        if (xlist.size() == 2) {
                            DailyData dailyData0 = (DailyData) xlist.get(0);
                            DailyData dailyData1 = (DailyData) xlist.get(1);

                            String avgString0 = dailyData0.getAvgcurnum();
                            String avgString1 = dailyData1.getAvgcurnum();
                            String maxString0 = dailyData0.getMaxcurnum();
                            String maxString1 = dailyData1.getMaxcurnum();

                            double avg0 = Double.parseDouble(avgString0);
                            double avg1 = Double.parseDouble(avgString1);
                            double max0 = Double.parseDouble(maxString0);
                            double max1 = Double.parseDouble(maxString1);

                            Double avg = Math.abs(avg0 - avg1);
                            Double max = Math.abs(max0 - max1);
                            DecimalFormat df = new DecimalFormat("0.00");
                            String avgresult = df.format(avg);
                            String maxresult = df.format(max);


                            daily.setDailyWaterAverage(avgresult);
                            daily.setDailyWaterMax(maxresult);
                            daily.setDataTime(dailyData0.getCurtime() + "~" + dailyData1.getCurtime());
                        }
                    }
                    dailyList.add(daily);
                }
            }
        }
        lwgyJointWaterDailyService.saveBatch(dailyList);
    }

}
